package com.neotech.neo4j.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.neo4j.shell.util.json.JSONArray;
import org.neo4j.shell.util.json.JSONException;
import org.neo4j.shell.util.json.JSONObject;

import com.neotech.neo4j.resources.Item;

/**
 * @author Slobodan
 * 
 * JSON util methods
 */
public class JSONUtil {

	public static String getDataFromJson(String json) {
		try {
			return new JSONObject(new JSONObject(json).get("data").toString())
					.toString();
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static ArrayList<String> getListByParameter(String responseString,
			String parameter) {
		ArrayList<String> dataList = new ArrayList<String>();
		try {
			if (responseString.startsWith("[")) {
				JSONArray ja;
				ja = new JSONArray(responseString);
				for (int i = 0; i < ja.length(); i++) {
					JSONObject jo = ja.getJSONObject(i);
					dataList.add(jo.getString(parameter));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataList;
	}
	
	public static ArrayList<Item> parseItemsFromJson(String responseString){
		ArrayList<Item> list = new ArrayList<Item>();
		try {
			if (responseString.startsWith("[")) {
				JSONArray ja = new JSONArray(responseString);
				for (int i = 0; i < ja.length(); i++) {
					JSONObject jo = ja.getJSONObject(i);
					Item item = new Item(getUriId(jo.toString()));
					item.setProperties(getPropertyList(jo.toString()));
					list.add(item);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static HashMap<String, String> getPropertyList(String responseString){
		HashMap<String, String> map = new HashMap<String, String>();
		ArrayList<String> dataList = new ArrayList<String>();
		try {
			JSONObject jo = (JSONObject) new JSONObject(responseString).get("data");
			Iterator<String> properties = jo.keys();
			while(properties.hasNext()){
				String property = properties.next();
				String value = jo.getString(property);
				map.put(property, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public static String getJsonProperty(String json, String key){
		try {
			JSONObject jo = new JSONObject(json);
			return jo.get(key).toString();
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static String generateJsonRelationship(String endNode,
			String relationshipType, String... jsonAttributes) {
		StringBuilder sb = new StringBuilder();
		sb.append("{ \"to\" : \"");
		sb.append(endNode);
		sb.append("\", ");

		sb.append("\"type\" : \"");
		sb.append(relationshipType);
		if (jsonAttributes == null || jsonAttributes.length < 1) {
			sb.append("\"");
		} else {
			sb.append("\", \"data\" : ");
			for (int i = 0; i < jsonAttributes.length; i++) {
				sb.append(jsonAttributes[i]);
				if (i < jsonAttributes.length - 1) { // Miss off the final comma
					sb.append(", ");
				}
			}
		}

		sb.append(" }");
		return sb.toString();
	}
	
	public static Long getUriId(String responseString){
		String self = getJsonProperty(responseString, "self");
		String[] split = self.split("/");
		String neoId = split[split.length-1];
		return new Long(neoId);
	}
}
